exports.get_bless_dice = function(player, req, resp, onHandled) {
    var user = player.user;
    var blessDice = user.bless_dice;
    if( !blessDice ) {
        blessDice = {
            'score' : 0,
            'count' : 0,
            'free'  : 0,
            'cash'  : 0,
            'cur_round' : 0,
        };

        user.bless_dice = blessDice;
        player.markDirty('bless_dice');
    }

    resp.data.bless_dice = blessDice; 

    onHandled();
};

exports.throw_dice = function(player, req, resp, onHandled) {
    do {
        var user = player.user; 
        var roles = user.role;
        
        var now = new Date();
        var openDays = now.getDayPassed(gConfGlobalServer.ServerStartTime);

        if (openDays < gConfGlobal.WingOpenDays) {
            resp.code = 1; resp.desc = 'not open'; break; 
        }

        var hasWing = false;
        for (var rid in roles) {
            if (roles[rid].god) {
                hasWing = true; 
                break;
            } 
        }

        if (!hasWing) {
            resp.code = 1; resp.desc = 'has not wing'; break; 
        }

        var blessDice = user.bless_dice;
        if( blessDice.cur_round ) {
            resp.code = 1; resp.desc = 'has throw'; break; 
        }

        if( blessDice.count >= gConfGlobal.BlessCountPerDay ) {
            resp.code = 1; resp.desc = 'day count limit'; break; 
        }

        var weights = {};        
        for( var id in gConfBlessDice ) {
            weights[id] = gConfBlessDice[id].Weight; 
        }
   
        var curRound = 0;
        for( var i = 1, max = gConfGlobal.BlessDiceCount; i <= max; ++i ) {
            var id = +common.wRand(weights);
            curRound = curRound * 10 + id; 
        }
        
        blessDice.count += 1;
        blessDice.cur_round = curRound;

        player.markDirty('bless_dice.count');
        player.markDirty('bless_dice.cur_round');

        resp.data.cur_round = blessDice.cur_round;
    } while(false);

    onHandled();
};

exports.get_bless_dice_reward = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        var blessDice = user.bless_dice;

        var curRound = blessDice.cur_round;
        if( !curRound ) {
            resp.code = 1; resp.desc = 'no throw'; break; 
        }
        
        var curRoundStr = curRound.toString();
        if( curRoundStr.length != gConfGlobal.BlessDiceCount ) {
            resp.code = 1; resp.desc = 'dice num error'; break; 
        }

        var blessCount = 0;
        for (var i = 0, len = curRoundStr.length; i < len; ++i) {
            if (1 == +(curRoundStr[i])) {
                blessCount++;
            }
        }

        var gainScore = gConfBlessDesc[blessCount].Score;

        if (blessCount >= gConfGlobal.BlessDiceCount) {
            gMessage.addSystemMessage('bless', [user.info.un, gainScore.toString()]);
        }

        blessDice.cur_round = 0;
        blessDice.cash = 0;
        blessDice.score += gainScore;
        
        player.markDirty('bless_dice.cur_round');
        player.markDirty('bless_dice.cash');
        player.markDirty('bless_dice.score');

        resp.data.score = gainScore;
    } while(false);

    onHandled();
};

exports.change_luck = function(player, req, resp, onHandled) {
    do {
        var isCash = req.args.cash ? true : false;

        var user = player.user; 
        var blessDice = user.bless_dice;

        var curRound = blessDice.cur_round;
        if( !curRound ) {
            resp.code = 1; resp.desc = 'no throw'; break; 
        }

        var curRoundStr = curRound.toString();
        if( curRoundStr.length != gConfGlobal.BlessDiceCount ) {
            resp.code = 1; resp.desc = 'num of dice error'; break; 
        }
       
        // 所有骰子是否均为福字
        if( (curRoundStr.replace(/[^1]/g,'').length == gConfGlobal.BlessDiceCount) ) {
            resp.code = 1; resp.desc = 'best now'; break; 
        }

        var costCash = 0;
        var addWeight = 0;
        if( !isCash ) {
            if( blessDice.free >= gConfGlobal.BlessFreeChangeTimes ) {
                resp.code = 1; resp.desc = 'free change day limit'; break; 
            } 

            blessDice.free += 1;
            player.markDirty('bless_dice.free');

            addWeight = gConfGlobal.BlessChangeFreeWeight * blessDice.free;

        }else {
            var nextCash = blessDice.cash + 1;
            var buyConf = gConfBuy[nextCash];
            if( !buyConf ) {
                resp.code = 1; resp.desc = 'no more to buy'; break; 
            }

            costCash = buyConf.CashBlessCost;
            if( !costCash ) {
                resp.code = 1; resp.desc = 'no more to buy'; break; 
            }

            if( !player.costResource('cash', costCash, blessDice.cash, blessDice.cur_round) ) {
                resp.code = 1; resp.desc = 'not enough cash'; break; 
            }

            blessDice.cash = nextCash;
            player.markDirty('bless_dice.cash');

            addWeight = gConfGlobal.BlessChangeCashWeight * nextCash;

            resp.data.cash = -costCash;
        }

        var newCurRound = curRoundStr;
        if( isCash && blessDice.cash >= gConfGlobal.BlessCashChangeTimes ) {
            newCurRound = newCurRound.replace(/[^1]/g,'1');

        }else {
            // 记录所有非福索引
            var changeIndexArr = []; 
            for( var i = 0, len = curRoundStr.length; i < len; ++i ) {
                if( +curRoundStr[i] != 1 ) {
                    changeIndexArr.push(i); 
                } 
            } 

            var weights = {};
            for( var id in gConfBlessDice ) {
                weights[id] = gConfBlessDice[id].Weight;
            }

            if( weights[1] ) {
                weights[1] += addWeight; 
            }
            
            for( var k = 0, len = changeIndexArr.length; k < len; ++k ) {
                var id = +common.wRand(weights);     
                var index = changeIndexArr[k];
                newCurRound = newCurRound.substring(0,index) + id + newCurRound.substring(index + 1, newCurRound.length);
            }
        }
        
        blessDice.cur_round = +newCurRound;
        player.markDirty('bless_dice.cur_round');

        resp.data.cur_round = +newCurRound;
    } while(false);

    onHandled();
};

exports.buy = function(player, req, resp, onHandled) {
    do {

        if( isNaN(req.args.id) || isNaN(req.args.count) ) {
            resp.code = 1; resp.desc = 'invalid args'; break; 
        }

        var id = Math.floor(req.args.id);
        var count = parseInt(req.args.count);
        if (count < 1 || count > 999) {
            resp.code = 1; resp.desc = 'args error'; break; 
        }
        
        var blessShopConf = gConfBlessShop[id];
        if( !blessShopConf ) {
            resp.code = 1; resp.desc = 'args error'; break; 
        }

        var user = player.user;
        var blessDice = user.bless_dice;

        var costScore = blessShopConf.Price * count;
        if( blessDice.score < costScore ) {
            resp.code = 1; resp.desc = 'not enough score'; break; 
        }

        blessDice.score -= costScore;
        player.markDirty('bless_dice.score');

        var awards = [];
        for( var i = 1, max = count; i <= count; ++i ) {
            awards.push( blessShopConf.Award1 ); 
        }

        resp.data.awards = player.addAwards(awards);
        resp.data.score = -costScore;
    } while(false);

    onHandled();
};
